package net.pinnoocle.playcommon.utils.wxPay;

import org.springframework.beans.factory.annotation.Value;

import java.io.*;

/**
 * @author ye
 * @Description
 * @create 2021-10-28 16:41
 */
public class MyConfig extends WXPayConfig {

    //小程序appId
    private String appId;

    //商户id
    private String mchId;

    //加密秘钥
    private String key;

    //证书路径
    private String certPath;

    //证书字节数据
    private final byte[] certData;

    public MyConfig(String appId,String mchId,String key,String certPath) throws IOException {

       this.appId = appId;
       this.mchId = mchId;
       this.key = key;
       File file = new File(certPath);
       InputStream certStream = new FileInputStream(file);
       this.certData = new byte[(int) file.length()];
       certStream.read(this.certData);
       certStream.close();
    }

    @Override
    public String getAppID() {
        return appId;//不能return this.appId,this会调用构造方法，构造方法里面调构造方法，就死循环了
    }

    @Override
    public String getMchID() {
        return mchId;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    @Override
    IWXPayDomain getWXPayDomain() {
        return new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new DomainInfo("api.mch.weixin.qq.com", false);
            }
        };
    }
}
